package br.edu.infnet.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.edu.infnet.hibernate.util.HibernateUtil;
import br.edu.infnet.modelo.Avaliacao;


public class AvaliacaoDAO {
	
	private Session session;
	private Query query;

	public void incluir(Avaliacao a) {
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.save(a);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
	}

	public void alterar(Avaliacao a) {
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.save(a);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
	}
	
	public void excluir(Avaliacao a) {
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.delete(a);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
	}

	public Avaliacao consultarAluno(String aluno) {
		Avaliacao retorno = new Avaliacao();
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			retorno = (Avaliacao) session.get(Avaliacao.class, aluno);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return retorno;
	}
	
	public Avaliacao consultarModulo(Integer modulo) {
		Avaliacao retorno = new Avaliacao();
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			retorno = (Avaliacao) session.get(Avaliacao.class, modulo);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return retorno;
	}

	public Avaliacao consultar(String aluno, Integer modulo) {
		Avaliacao retorno = new Avaliacao();
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			query = session.createQuery("from Avaliacao a where a.idLogin = : aluno and a.idModulo = : modulo");
			query.setString("aluno", aluno);
			query.setLong("modulo", modulo);
			if (!query.list().isEmpty()){
				retorno = (Avaliacao) query.list() ;
			}else{
				retorno = null;
			}
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return retorno;
	}

	@SuppressWarnings("unchecked")
	public List<Avaliacao> listar() {
		List<Avaliacao> lista = new ArrayList<Avaliacao>();
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			lista = (List<Avaliacao>) session.createCriteria(Avaliacao.class).list();
			session.getTransaction().commit();
		} catch (Exception e) {			
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return lista;
	}

	@SuppressWarnings("unchecked")
	public List<Avaliacao> listar(Avaliacao a) {
		Criteria c = session.createCriteria(Avaliacao.class);
		if (a.getIdLogin().toString().length() > 0) {
			c.add(Restrictions.like("nome", a.getIdLogin() + "%"));
		}
		c.addOrder(Order.asc("nome"));
		return (List<Avaliacao>) c.list();
	}

}
